package your.rfgui.extgui.controller.table;

import java.util.List;
import org.apache.log4j.Logger;
import your.rfgui.extgui.controller.tree.data.LabelDataItem;
import your.rfgui.extgui.data.intf.DataItem;
import your.rfgui.extgui.data.intf.GuiParentChildEntity;
import your.rfgui.extgui.service.intf.GuiParentChildService;

public class ParentChildTableController<T extends GuiParentChildEntity<T>> extends AbstractTableController {

   private static final Logger log = Logger.getLogger(ParentChildTableController.class);
   GuiParentChildService<T> entityService;

   public ParentChildTableController(GuiParentChildService<T> entityService) {
      log.info("ParentChildTableController");
      this.entityService = entityService;
      setListParent(null);
   }

   /**
    * Returns the children data item list for the given data item
    */
   @Override
   List<? extends DataItem> getViewChildren(DataItem dataItem) {
      log.trace("getViewChildren");

      if (dataItem == null || dataItem instanceof LabelDataItem) {
         return entityService.getRootList();
      } else {
         assert (dataItem instanceof GuiParentChildEntity);
         @SuppressWarnings("unchecked")
         List<T> childrenList = ((GuiParentChildEntity<T>) dataItem).getChildren();
         return childrenList;
      }
   }
}
